//
//  WmcWatermarkPreview.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/10.
//

import UIKit

/// 水印编辑实时预览
open class WmcEditWatermarkPreview: UIScrollView {
    // Lifecycle

    override public init(frame: CGRect) {
        super.init(frame: frame)

        makeUI()
    }

    @available(*, unavailable)
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Open

    /// 边距
    open var inset: UIEdgeInsets = .YP.Create(horizontal: 0, vertical: 12)

    override open func layoutSubviews() {
        super.layoutSubviews()
        guard let watermarkView = watermarkView else { return }

        let insetTop: CGFloat = 12

        var brandHeight: CGFloat = 0
        if let brandView = brandView {
            brandHeight = brandView.frame.height

            let brandX = watermarkView.frame.origin.x + 8
            let brandY = insetTop
            brandView.frame = .init(x: brandX, y: brandY, width: brandView.frame.width, height: brandView.frame.height)
        }

        watermarkView.snp.remakeConstraints { make in
            let centerTop = (watermarkView.frame.height / 2) + insetTop + brandHeight
            make.centerY.equalTo(centerTop)
            make.centerX.equalToSuperview()
            if let width = watermarkView.defaultWidth {
                make.width.equalTo(width)
            }
        }

        contentSize = CGSize(width: Wmc_screenWidth,
                             height: insetTop + brandHeight + watermarkView.frame.height + insetTop)
    }

    /// 设置需要渲染的水印模型
    open func setWatermarkModel(_ model: WmcWatermarkDetailModel,
                                styleMode: WmcBaseWatermarkTemplateView.StyleMode = .dynamicEdit)
    {
        DispatchQueue.main.async { [weak self] in
            guard let self = self else { return }
            if self.watermarkView?.watermarkModel?.templateId != model.templateId { // 模版变化，重新创建
                self.watermarkView?.removeFromSuperview()
                self.watermarkView = nil
                // 设置水印视图
                // 不显示认证水印
                guard let templateView = model.createTemplateView(styleMode: styleMode,
                                                                  showCertification: false) else { return }
                self.watermarkView = templateView
            }
            guard let templateView = self.watermarkView else { return }
            
            templateView.configWatermarkModel(model, styleMode: styleMode)
            
            self.addSubview(templateView)
            
            // 品牌图属性
            self.brandView?.removeFromSuperview()
            self.brandView = nil
            if templateView.editPreviewBrandContentShowBrand, // 询问模版是否需要展示品牌
               let optionBrand = model.optionBrand(),
               optionBrand.isOpen
            {
                let brand = optionBrand.brand
                if brand.locationType == .followWatermark,
                   let brandView = WmcBaseBrandView.CreateBrandTemplateView(optionBrand)
                {
                    brandView.configOptionBrand(optionBrand)
                    self.addSubview(brandView)
                    self.brandView = brandView
                }
            }
            self.layoutSubviews()
        }
    }

    // Internal

    /// 水印视图
    var watermarkView: WmcBaseWatermarkTemplateView?

    /// 品牌图预览
    var brandView: WmcBaseBrandView?

    func makeUI() {
        backgroundColor = .wmc_grayH4_5E6063
    }
}
